package com.py.article.service.impl;

import com.py.article.service.ArticleLoadBehaviorService;
import com.py.common.redis.CacheService;
import com.py.model.article.dtos.ArticleInfoDto;
import com.py.model.article.dtos.UserArticleInfoDto;
import com.py.model.common.dtos.ResponseResult;
import com.py.utils.common.ApUserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName ArticleLoadBehaviorServiceImpl
 * @Description TODO
 * @Author AXing
 * @Date 2022/11/17 14:37
 * @Version 1.0
 */
@Service
@Slf4j
public class ArticleLoadBehaviorServiceImpl implements ArticleLoadBehaviorService {
    @Autowired
    private CacheService cacheService;
    /**
     * 加载文章对应行为信息
     * @param dto
     * @return
     */
    @Override
    public ResponseResult articleBehavior(ArticleInfoDto dto) {
        //文章id
        Long articleId = dto.getArticleId();
        //作者id
        Integer authorId = dto.getAuthorId();
        //用户id
        Integer userId = ApUserThreadLocalUtil.get().getId();
        //用户对应文章key
        String userArticleKey = articleId + "_" + userId;
        //用户对应作者key
        String userAuthorKey = authorId + "_" + userId;

        UserArticleInfoDto infoDto = new UserArticleInfoDto();

        //查询是否点赞文章
        String likes = (String) cacheService.hGet(userArticleKey, "likes");
        log.info("likes---->{}", likes);
        if (likes == null || "0".equals(likes)){
            //没点赞文章
            infoDto.setIslike(false);
        } else {
            //点赞了该文章
            infoDto.setIslike(true);
        }

        //查询是否不喜欢文章
        String unlikes = (String) cacheService.hGet(userArticleKey, "unLikes");
        log.info("unlikes---->{}", unlikes);
        if (unlikes == null || "0".equals(unlikes)){
            //没有不喜欢文章
            infoDto.setIsunlike(false);
        } else {
            //不喜欢该文章
            infoDto.setIsunlike(true);
        }

        //查询是否收藏文章
        String collection = (String) cacheService.hGet(userArticleKey, "collection");
        log.info("collection---->{}", collection);
        if (collection == null || "0".equals(collection)){
            //没有收藏文章
            infoDto.setIscollection(false);
        } else {
            //收藏该文章
            infoDto.setIscollection(true);
        }

        //查询是否关注该作者
        String follow = (String) cacheService.hGet(userAuthorKey, "follow");
        log.info("follow----->{}", follow);
        if (follow == null || "0".equals(follow)){
            //没有关注该作者
            infoDto.setIsfollow(false);
        } else {
            //收藏该作者
            infoDto.setIsfollow(true);
        }
        return ResponseResult.okResult(infoDto);
    }
}
